/**
 * Copyright (c) 2002-2011 旭鸣信息
 * All rights reserved. 
 * File：GoodsTypeContorller.java
 * History:
 *       May 13, 2012: Initially created, liuzhi.
 */
package com.xteam.ecsystem.contorller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tangkf.utils.bean.ReqHelper;
import com.xteam.ecsystem.entities.BaseTypeAttribute;
import com.xteam.ecsystem.service.admin.GoodsTypeAttrService;
import com.xteam.ecsystem.service.admin.SystemLogService;
import com.xteam.ecsystem.utils.ConstantLocation;
import com.xteam.ecsystem.utils.StringUtil;

/**
 * 商品类型属性控制枢纽站
 * @author liuzhi
 *
 */
@Controller
public class GoodsTypeAttrContorller {

	@Autowired
	private GoodsTypeAttrService typeAttrService;
	
	@Autowired
	private SystemLogService logService;
	
	public void setToBeJsonType(HttpServletRequest request, HttpServletResponse response) {
		response.setContentType("application/json;charset=UTF-8");
	};
	
	@RequestMapping("/getGoodsTypeAttrList.do")
	public void getGoodsTypeAttrList(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		
		int pageIndex = ReqHelper.getRequestInt(request, "pageIndex");
		int pageSize = ReqHelper.getRequestInt(request, "pageSize");
		String btaGtype = ReqHelper.getRequestString(request, "btaGtype");
		
		String strList = this.typeAttrService.getGoodsTypeAttrList(pageIndex, pageSize, btaGtype);
		response.getWriter().write(strList);
	}
	
	@RequestMapping("/saveOrUpdateGoodsTypeAttr.do")
	public void saveOrUpdateObject(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String jsonStr = ReqHelper.getRequestString(request, "goodsTypeAttrInfo");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		String lslType = ConstantLocation.LOG_ADD;
		
		JSONObject obj = JSONObject.fromObject(jsonStr);
		BaseTypeAttribute typeAttr = (BaseTypeAttribute)JSONObject.toBean(obj, BaseTypeAttribute.class);
		
		if(StringUtil.isNotNullOrEmpty(typeAttr.getBtaId())){
			lslType = ConstantLocation.LOG_MODIFY;
		}
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_TYPEATTR_MGR, 
				"saveOrUpdateObject", lslType, "BaseTypeAttribute");
		
		this.typeAttrService.saveOrUpdateObject(typeAttr, typeAttr.getBtaId());
	}
	
	@RequestMapping("/getGoodsTypeAttrById.do")
	public void getGoodsTypeAttrById(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		
		String btaId = ReqHelper.getRequestString(request, "btaId");		
		String strInfo = this.typeAttrService.getGoodsTypeAttrById(btaId);
		
		response.getWriter().write(strInfo);
	}
	
	@RequestMapping("/updateBtaStateById.do")
	public void updateBtaStateById(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String btaId = ReqHelper.getRequestString(request, "btaId");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");

		this.typeAttrService.updateBtaStateById(btaId);
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_TYPEATTR_MGR, 
				"updateBtaStateById", ConstantLocation.LOG_Edit_STATE, "BaseTypeAttribute");

	}
	
	@RequestMapping("/findGoodsTypeJson.do")
	public void findGoodsTypeJson(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);

		String type = ReqHelper.getRequestString(request, "type");		
		String goodsTypeJson = this.typeAttrService.findGoodsTypeJson(type);
		
		response.getWriter().write(goodsTypeJson);

	}
	
	@RequestMapping("/delGoodsTypeAttrByIds.do")
	public void delGoodsTypeAttrByIds(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String btaIds = ReqHelper.getRequestString(request, "btaIds");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");

		this.typeAttrService.delGoodsTypeAttrByIds(btaIds);
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_TYPEATTR_MGR, 
				"delGoodsTypeAttrByIds", ConstantLocation.LOG_DELETE, "BaseTypeAttribute");

	}
	
	@RequestMapping("/getBgaByBta.do")
	public void getBgaByBta(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		
		String btaIds = ReqHelper.getRequestString(request, "btaIds");

		boolean isUsed = this.typeAttrService.getBgaByBta(btaIds);
		if (isUsed) {
			response.getWriter().write("1");
		}else{
			response.getWriter().write("0");
		}
	}
	
}
